//
//  ApplicationController.swift
//  QualityControlSystem
//
//  Created by dundun on 17/1/17.
//  Copyright © 2017年 com.mask. All rights reserved.
//

import UIKit

class ApplicationController: UITableViewController {
    
    var alreadyDataArr: [ApplicationModel]?
    var moreDataArr: [ApplicationModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = Color.bgGray
        tableView.alwaysBounceVertical = true
        tableView.register(ApplicationTopCell.self, forCellReuseIdentifier: "applicationTopCell")
        tableView.register(AlreadyApplicationCell.self, forCellReuseIdentifier: "alreadyApplicationCell")
        loadData()
    }
    
    func loadData() {
        let user = User.shared
        HTTPRequest.applicationsInfo(username: user.name!) { (response) in
            self.alreadyDataArr = response["data"] as! [ApplicationModel]?
            self.tableView.reloadData()
        }
    }
    
    // MARK: - Table view dataSource
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 2 {
            return 0
        } else {
            return 1
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let topCell = tableView.dequeueReusableCell(withIdentifier: "applicationTopCell", for: indexPath) as? ApplicationTopCell
            topCell?.topImage?.image = UIImage(named: "banner")
            return topCell!
        } else {
            let alreadyApplicationCell = tableView.dequeueReusableCell(withIdentifier: "alreadyApplicationCell", for: indexPath) as? AlreadyApplicationCell
            alreadyApplicationCell?.data = alreadyDataArr
            alreadyApplicationCell?.selectApplicationBlock = { (model) in
                //点击选择应用
                print("点击")
            }
            return alreadyApplicationCell!
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            var nilHeader = tableView.dequeueReusableHeaderFooterView(withIdentifier: "nilHeader")
            if nilHeader == nil {
                nilHeader = UITableViewHeaderFooterView.init(reuseIdentifier: "nilHeader")
            }
            return nilHeader
        } else {
            var header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "appHeader") as? ApplicationHeader
            if header == nil {
                header = ApplicationHeader.init(reuseIdentifier: "appHeader")
            }
            if section == 1 {
                header?.headerTitle?.text = "已有应用"
                header?.headerClickedBlock = { () in
                    //点击已有应用跳转
                    print("已有应用")
                }
            } else {
                header?.headerTitle?.text = "更多应用"
                header?.headerClickedBlock = { () in
                    //点击更多应用跳转
                    print("更多应用")
                }
            }
            return header!
        }
    }
    
    
    
    // MARK: - Tableview delegate
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0.1
        } else {
            return 35.0
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 130
        } else if indexPath.section == 1 {
            return 120
        } else {
            return 0
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 0 {
            return Padding.top
        } else {
            return 0.1
        }
    }
    
    override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
        return false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
